"""satck  based on linked list

"""

from typing import Optional


class Node:

    def __init__(self, data: int, next=None):
        self._data = data
        self._next = next


class LinkedStack():
    '''
    A stack BASED ON THE SINGLE-LINKED -LIST
    '''

    def __init__(self):
        self._top: Node = None

    def push(self, value):
        new_top = Node(value)
        new_top._next = self._top
        self._top = new_top

    def pop(self) -> Optional[int]:
        if self._top:
            value = self._top._data
            self._top = self._top._next
            return value

    def __repr__(self) -> str:
        current = self._top
        nums = []
        while current:
            nums.append(current._data)
            current = current._next
        return " ".join(f'{num}' for num in nums)


if __name__ == "__main__":
    stack = LinkedStack()
    for i in range(9):
        print('pushing ' + str(i))
        stack.push(i)
    print(stack)
    for _ in range(3):
        print("poping " + str(stack.pop()))
    print(stack)
